<template>
  <div class="comment">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.com_id"
      >
        <!-- 自定义插槽复用 -->
        <template #title>
          <div class="cmt-wrap">
            <van-image
              round
              fit="cover"
              :src="item.aut_photo"
            ></van-image>
            <div class="info">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">{{ item.pubdate | relvTime
                }}<van-button
                  size="mini"
                  @click="showReply(item)"
                >回复({{ item.reply_count }})</van-button>
              </div>
            </div>
            <van-icon
              :name="item.is_liking?'like':'like-o'"
              class="red"
              @click="likeHart(item)"
              ref="red"
            />
          </div>
        </template>
      </van-cell>
    </van-list>
    <div class="cmt-input-wrap">
      <!-- 输入的区域 -->
      <van-field
        v-model="cmtValue"
        center
        clearable
        @keyup.enter="send"
        placeholder="请输入评论内容"
      >
        <template #button>
          <van-button
            size="small"
            type="info"
            @click="send"
          >发布</van-button>
        </template>
      </van-field>
    </div>
    <van-action-sheet
      v-model="sheetShow"
      title="标题"
    >
      <reply
        :currentItem='currentItem'
        v-if="sheetShow"
      />
    </van-action-sheet>
  </div>
</template>

<script>
import { cmtListAPI, sendCmtAPI } from "@/api";
import reply from './reply.vue'
export default {
  name: 'comment',
  data() {
    return {
      list: [],
      loading: false,
      finished: false,

      // 输入的评论内容
      cmtValue: "",
      offset: undefined,

      sheetShow: false, // 发表评论的评论
      currentItem: '',  // 当前点击的评论
    };
  },
  components: {
    reply
  },
  methods: {
    // 点击回复
    showReply(item) {
      this.sheetShow = true
      this.currentItem = item

    },
    // 渲染评论列表信息
    async onLoad() {
      const res = await cmtListAPI({
        type: "a",
        source: this.$route.query.id, //index页面拿了id了 （一个页面的地址上id可以拿到）
        offset: this.offset,
      });
      this.list.push(...res.data.data.results)
      // 把本次的最后一条id作为下次请求的参数
      this.offset = res.data.data.last_id
      this.loading = false

      //  当本次的最后一条id，等于 所有数据的结束id就意味着加载完了
      if (res.data.data.end_id == res.data.data.last_id) {
        this.finished = true;
      }

    },
    // 点击发送 
    async send() {
      //发表评论
      if (this.cmtValue === '') {
        this.$toast('请输入内容')
        return
      }
      const res2 = await sendCmtAPI({
        content: this.cmtValue,
        target: this.$route.query.id, // 文章id
      })
      this.list.unshift(res2.data.data.new_obj)
      this.cmtValue = ''
      this.$toast('发表评论成功')
    },
    likeHart(item) {
      // this.$refs.red.is_liking = !this.$refs.red.is_liking
      // this.like = this.$refs.red.is_liking
      console.log('item:', item);
      if (item.is_liking) {
        item.is_liking = false
      } else {
        item.is_liking = true
      }
    }
  },
};
</script>


<style lang="less" scoped>
.comment {
  margin-top: 20px;
  ::v-deep.cmt-wrap {
    display: flex;
    .van-image {
      width: 45px;
      height: 45px;
    }
    .info {
      margin-left: 20px;
      flex: 1;
      .name {
        color: #466b9d;
      }

      .content {
        margin: 8px 0;
      }

      .time {
        height: 20px;
        display: flex;
        align-items: center;
        color: gray;
      }
    }
    .red {
      color: red;
    }
  }

  .cmt-input-wrap {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }
}
</style>